package com.jn.erp.web.api.student.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.jn.erp.common.core.page.TableDataInfo;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.security.controller.BaseWebAuthController;
import com.jn.erp.web.api.student.entity.TRenewalAlertsConfig;
import com.jn.erp.web.api.student.entity.dto.StudentRenewalDto;
import com.jn.erp.web.api.student.service.RenewalAlertsConfigService;
import com.jn.erp.web.api.student.service.StudentService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/renewalAlerts")
public class RenewalAlertsController extends BaseWebAuthController {

    @Autowired
   private StudentService studentService;

    @Autowired
   private RenewalAlertsConfigService renewalAlertsConfigService;

    /**
     * 预警列表
     */
    @GetMapping("/alertsList")
    public TableDataInfo auditionList(StudentRenewalDto studentRenewalDto) {
        studentRenewalDto.setOrgId(Long.parseLong(loginDeptAuth()));
        startPage();
        List<StudentRenewalDto> list = studentService.alertsList(studentRenewalDto);
        return getDataTable(list);
    }

    /**
     * 预警设置
     */
    @PostMapping("/alertSettings")
    public AjaxResult alertSettings(@RequestBody StudentRenewalDto studentRenewalDto) {
        studentRenewalDto.setOrgId(Long.parseLong(loginDeptAuth()));
        renewalAlertsConfigService.alertSettings(studentRenewalDto);
        return success("设置成功");
    }

    /**
     * 预警设置列表
     */
    @PostMapping("/settingsList")
    public AjaxResult settingsList(TRenewalAlertsConfig tRenewalAlertsConfig) {
        tRenewalAlertsConfig.setOrgId(Long.parseLong(loginDeptAuth()));
        TRenewalAlertsConfig setting =  renewalAlertsConfigService.settingsList(tRenewalAlertsConfig);
        return success(setting);
    }





    @RequestMapping("/alertExport")
    public void alertExport(@RequestBody StudentRenewalDto studentRenewalDto, HttpServletResponse response) {
        studentRenewalDto.setPageSize(999999999);
        studentRenewalDto.setPageNum(1);
        //1.查询数据
        // 2在读 5历史
        studentRenewalDto.setOrgId(Long.parseLong(loginDeptAuth()));
        List<StudentRenewalDto> list = studentService.alertsList(studentRenewalDto);

        //2.封装成表格
        //参数1：表格标题，参数2：sheet名称
        ExportParams exportParams = new ExportParams(null, "续费预警学员导出");

        //参数1：表格参数  参数2：实体类  参数3：数据
        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, StudentRenewalDto.class, list);
        //3.返回表格
        //设置表格文件名字
        String fileName = "续费预警学员导出";
        try {
            fileName = URLEncoder.encode(fileName, "UTF8");
            //设置返回数据类型
            response.setHeader("Access-Control-Expose-Headers", "content-disposition");
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");

            //将表格输出
            OutputStream os = response.getOutputStream();
            sheets.write(os);
            os.close();
            os.flush();
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
